########## Supplementary materials 3 #############

## This code replicates all results from section 3 of the supplementary materials
## This includes extended results tables for the Afrobarometer analyses presented in the main text


rm(list=ls())

library(tidyverse)
library(fixest)

## Load AB data

rm(list=ls())
AB_malawi <- readRDS("AB_malawi.rds")

## Run analysis and store results

AB1.1 <- feols(own_phone*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)

AB1.2 <- feols(smartphone_bin*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)

AB1.3 <- feols(socmedia_news_bin*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)

AB1.4 <- feols(internet_news_bin*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)

AB1.5 <- feols(socmedia_heard*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)


#### TABLE 47 (Validating coverage) #####

## Set fixest dictionary
setFixest_dict(`group::Alwayscovered` = "Always inside", 
               `group::Entercoverage` = "Enter coverage", 
               district = "District", 
               ea_code = "Enumeration area", 
               `own_phone*100` = "Own phone", 
               `smartphone_bin*100` = "Own smartphone", 
               `socmedia_news_bin*100` = "Social media news", 
               `internet_news_bin*100` = "Internet news", 
               `socmedia_heard*100` = "Heard of social media", 
               `election_contact*100` = "Any contact", 
               `contact_dpp*100` = "DPP", 
               `contact_mcp*100` = "MCP", 
               `trust_ruling_std*100` = "DPP", 
               `performance_pres_std*100` = "President")

## Make table (note: this table was complied in RMarkdown, with the chunk option: results = "asis")
etable(AB1.1, AB1.2, AB1.3, 
       AB1.4, AB1.5, 
       keep = c("%group"),
       tex=F, 
       fontsize = "scriptsize", 
       title = "Validating coverage with geocoded Afrobarometer survey data")

#### TABLE 47 (Contact and incumbent support) #####

ABcontact <- AB_malawi %>% 
  filter(election_contact == "1") %>%
  mutate(contact_dpp = ifelse(is.na(contact_dpp) == "TRUE", 0, contact_dpp),
         contact_mcp = ifelse(is.na(contact_mcp) == "TRUE", 0, contact_mcp))

AB2.1 <- feols(election_contact*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)

AB2.2 <- feols(contact_dpp*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)

AB2.3 <- feols(contact_mcp*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)

AB2.4 <- feols(contact_dpp*100 ~ i(group, ref="Control") + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=ABcontact, 
               cluster=~ea_code) ## Note: uses "among contacted" dataset

AB2.5 <- feols(contact_mcp*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=ABcontact, 
               cluster=~ea_code) ## Note: uses "among contacted" dataset

AB2.6 <- feols(trust_ruling_std*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)

AB2.7 <- feols(performance_pres_std*100 ~ i(group, ref="Control")  + 
                 i(partisanship, ref = "Independent") + 
                 age + male + education + urbrur + i(ethnicity) +
                 hh_basket | district,
               data=AB_malawi, 
               cluster=~ea_code)


etable(AB2.1, 
       AB2.2, AB2.3, 
       AB2.4, AB2.5, 
       AB2.6, AB2.7,
       keep = c("%group"),
       headers = list("^:_:Election" = list("Full sample" = 1, 
                                            "Party vs no contact" = 2,
                                            "Party vs other parties" = 2,
                                            "Incumbent support" = 2)),
       tex=F, 
       fontsize = "scriptsize", 
       title = "Party contact and incumbent support")


